﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace postSale.forms
{
    public partial class processEdit : taskEdit 
    {
        public processEdit()
        {
            try
            {
                InitializeComponent();
                this.myValidTaskTypeList = new myTypes.taskType[] { myTypes.taskType.User_Immediate, myTypes.taskType.Auto};
                this.myValidTaskStatusList = new common.CommonStatus[] { common.CommonStatus.New, common.CommonStatus.Processing };

                onDateEd.BackColor = CommonTypes.Settings.sysColorDisableBG;
                saleCodeEd.BackColor = CommonTypes.Settings.sysColorDisableBG;
                custCodeEd.BackColor = CommonTypes.Settings.sysColorDisableBG;
                refTasIdEd.BackColor = CommonTypes.Settings.sysColorDisableBG;
                noteEd1.BackColor = CommonTypes.Settings.sysColorDisableBG;
                noteEd2.BackColor = CommonTypes.Settings.sysColorDisableBG;
                workContactEd.BackColor = CommonTypes.Settings.sysColorDisableBG;
                workAddressEd.BackColor = CommonTypes.Settings.sysColorDisableBG;
                workByIdEd.BackColor = CommonTypes.Settings.sysColorDisableBG;
                workByNameEd.BackColor = CommonTypes.Settings.sysColorDisableBG;
                
                cbStatus.BackColor = CommonTypes.Settings.sysColorDisableBG;
                cbTaskType.BackColor = CommonTypes.Settings.sysColorDisableBG;
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }

        public void ShowProcess(string taskId,bool dialogMode)
        {
            this.ShowMessage("");
            taskTA.FillByTaskId(myPostSalesDS.tasks, taskId);
            this.Show(); this.BringToFront();
            ShowCustomer();
            ShowWorkByName();
            if (dialogMode)
            {
                this.Hide();
                this.ShowDialog();
            }
        }
        public override void LockEdit()
        {
            base.LockEdit();
            bool lockState = this.isLockEdit;

            companyCb.Enabled = false;
            cbTaskType.Enabled = false;
            sendEmailBtn.Enabled = !lockState; 
            cbStatus.Enabled = !lockState;
            workBySelectBtn.Enabled = !lockState;
            workByRemoveBtn.Enabled = !lockState;
         
            onDateEd.Enabled = true;
            saleCodeEd.Enabled = true;
            custCodeEd.Enabled = true;
            itemSeriEd.Enabled = true;
            refTasIdEd.Enabled = true;
            noteEd1.Enabled = true;
            noteEd2.Enabled = true;
            workContactEd.Enabled = true;
            workAddressEd.Enabled = true;
            workByIdEd.Enabled = true;
            workByNameEd.Enabled = true;
        }
        protected override void LoadData()
        {
            base.LoadData();
            if (processWorkDetail.Visible) processWorkDetail.LoadData();
        }

        public override void AddNew(string code)
        {
            common.SysLibs.ShowErrorMessage("Không thể thêm dữ liệu.");
            return;
        }
        protected override void RemoveCurrent()
        {
            common.SysLibs.ShowErrorMessage("Không thể xóa dữ liệu.");
            return;
        }
        protected override void SetFirstFocus()
        {
            this.cbStatus.Focus();
        }
       
        protected void ShowWorkByName()
        {
            workByNameEd.Text="";
            int workerId=0;
            if (!int.TryParse(workByIdEd.Text,out workerId)) return;
            ShowWorkByName(DataAccess.DataLibs.GetWorkerById(workerId));
        }
        private void ShowWorkByName(data.masterDS.workerRow workerRow)
        {
            workByNameEd.Text = "";
            if (workerRow==null) return;
            workByNameEd.Text = workerRow.login.Trim() + "," + workerRow.lname.Trim() + " " + workerRow.fname.Trim();
        }

        private void tasksSource_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                ShowWorkByName();
                if (workDetailPnl.Visible) processWorkDetail.LoadData();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void workBySelectBtn_Click(object sender, EventArgs e)
        {
            try
            {
                application.dataFind.myWorkerFind.Find(common.Settings.sysFindMarker.ToString());
                if (application.dataFind.myWorkerFind.selectedDataRow == null) return;
                data.masterDS.workerRow workerRow = (data.masterDS.workerRow)application.dataFind.myWorkerFind.selectedDataRow;
                workByIdEd.Text = workerRow.workerId.ToString();
                ShowWorkByName(workerRow);
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void sendEmailBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                if (tasksSource.Current == null) return;
                data.postSaleDS.tasksRow taskRow = (data.postSaleDS.tasksRow)((DataRowView)tasksSource.Current).Row;
                if (taskRow.IsworkByNull())
                {
                    common.SysLibs.ShowErrorMessage("Xin vui lòng phân công và lưu dữ liệu trước khi thực hiện.");
                    return;
                }
                string body = postSale.SysLibs.MakeMessgeBody(postSale.consts.constTplFile_TaskNotify, "", "", taskRow);
                if (postSale.SysLibs.Send_Notify_ViaEmail(new int[]{taskRow.workBy}, "Thông báo", body))
                    this.ShowMessage("Đã gởi mail đến : " + workByNameEd.Text);
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void workDetailCloseBtn_Click(object sender, EventArgs e)
        {
            workDetailPnl.Hide();
            showWorkDetailBtn.Enabled = true;
        }
        private void showWorkDetailBtn_Click(object sender, EventArgs e)
        {
            if (tasksSource.Current == null) return;
            processWorkDetail.LoadData();
            workDetailPnl.Show(); workDetailPnl.BringToFront();
            showWorkDetailBtn.Enabled = false;
        }
        private void workByRemoveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (tasksSource.Current == null || workByIdEd.Text.Trim()=="") return;
                if (MessageBox.Show("Hủy phân công cho ["+ workByNameEd.Text.Trim() +"] ?", CommonTypes.Consts.constApplicationName, MessageBoxButtons.YesNo) == DialogResult.No)
                    return;
                data.postSaleDS.tasksRow taskRow = (data.postSaleDS.tasksRow)((DataRowView)tasksSource.Current).Row;
                taskRow.SetworkByNull(); 
                //workByIdEd.Text = "";
                workByNameEd.Text = "";
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
    }
}
